<?php
// +----------------------------------------------------------------------
// | Desc: [ Better-充值审核 ]
// +----------------------------------------------------------------------
// | Author: huangzhen_20210406
// +----------------------------------------------------------------------

namespace app\common\model;
use app\api\controller\PublicApi;
use think\facade\Db;
use app\api\controller\Index;

class CardsTransOne extends Common
{
    //---------------------------------------------后端---------------------------------------------------------------
    public function tableDataAdmin($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where ="1=1";
        if(isset($post['user_name']) && !empty($post['user_name'])){
            //$where[] = ['username', '=', trim($post['user_name'])];
            $where .= " and username ='" . trim($post['user_name']) . "'";
        }
        if(isset($post['card_num']) && !empty($post['card_num'])){
            //$where[] = ['ct.card_num', '=', trim($post['card_num'])];
            $where .= " and ct.card_num ='" . trim($post['card_num']) . "'";
        }
        if(isset($post['desc']) && !empty($post['desc'])){
            //$where[] = ['ct.desc', 'like', '%' . trim($post['desc']) . '%'];
            $where .= " and ct.desc like '" . '%' . trim($post['desc']) . '%' . "'";
        }
        if(isset($post['card_type']) && !empty($post['card_type'])){
            //$where[] = ['ct.desc', 'like', '%' . trim($post['desc']) . '%'];
            $where .= " and ct.card_num like '" . trim($post['card_type']) . '%' . "'";
        }
        if(isset($post['tran_status']) && !empty($post['tran_status'])){
            //$where[] = ['ct.tran_status', '=', trim($post['tran_status'])];
            $where .= " and ct.tran_status ='" . trim($post['tran_status']) . "'";
        }
        if(isset($post['appCode']) && !empty($post['appCode'])){
            //$where[] = ['ct.tran_status', '=', trim($post['tran_status'])];
            $where .= " and ct.app_code ='" . trim($post['appCode']) . "'";
        }
        if(isset($post['reversal_flag']) && !empty($post['reversal_flag'])){
            //$where[] = ['ct.reversal_flag', '=', $post['reversal_flag']];
            $where .= " and ct.reversal_flag ='" . $post['reversal_flag'] . "'";
        }
        if(isset($post['amount']) && !empty($post['amount'])){
            //$where[] = ['ct.shopping', 'like', '%' . trim($post['shopping']) . '%'];
            $where .= " and ct.amount_auth = " . trim($post['amount']);
        }
        if(isset($post['shopping']) && !empty($post['shopping'])){
            //$where[] = ['ct.shopping', 'like', '%' . trim($post['shopping']) . '%'];
            $where .= " and ct.shopping like '" . '%' . trim($post['shopping']) . '%' . "'";
        }
        if(isset($post['card_token']) && !empty($post['card_token'])){
            $card_num = Db::name('cards')->where(['card_token'=>trim($post['card_token'])])->value('card_num');
            $where .= " and ct.card_num ='" . $card_num . "'";
        }
        if(isset($post['bank_type']) && strlen($post['bank_type'])>0){
            //$where[] = ['ct.bank_type', '=', $post['bank_type']];
            if($post['bank_type'] == 3){
                $where .= " and ct.bank_type = 3";
            }else{
                $where .= " and ct.bank_type != 3";
            }
        }
        if(isset($post['Credit_556766']) && strlen($post['Credit_556766'])>0){
            if($post['Credit_556766'] == 1){
                $where .= " and substr(card_num,1,6) != '556766'";
            }else{
                $where .= " and substr(card_num,1,6) = '556766'";
            }
        }

        if(isset($post['datetime']) && !empty($post['datetime'])){
            $time_list = explode(' _ ' , $post['datetime']);
            $start_time = $time_list[0] . " 00:00:00";
            $end_time   = $time_list[1] . " 23:59:59";
            //$where[] = ['ct.ctime', 'between', "{$start_time},{$end_time}"];
            $where .= " and (ct.ctime between '{$start_time}' and '{$end_time}')";
        }
        $list = Db::name('cards_trans_one')->alias('ct')
            ->leftJoin('user u' , 'u.id=ct.user_id')
            ->field('u.username,ct.*')
            ->order('ct.ctime desc')
            ->where($where)
            ->paginate(['list_rows' => $list_rows, 'page' => $page])
            ->toArray();
        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }
}